package com.caosy.grassthinkremote.controller;

import cn.hutool.core.collection.CollUtil;
import com.caosy.grassthinkremote.common.RespData;
import com.caosy.grassthinkremote.util.SortUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/interview")
public class InterviewController {

    @GetMapping("/quickSort")
    public RespData quickSort(String nums) {
        Integer[] numArr = getNumArrs(nums);
        SortUtil.quickSort(numArr,0,numArr.length - 1);
        return RespData.getSuccess(Arrays.asList(numArr));
    }

    private Integer[] getNumArrs(String nums) {
        String[] numStrs = nums.split(",");
        List<Integer> list = Arrays.stream(numStrs)
                .map(str -> Integer.parseInt(str)).collect(Collectors.toList());
        return list.toArray(new Integer[0]);
    }

    /**
     * 小顶堆的排序最终排序出来的是倒序，因此要反转
     * @param nums
     * @return
     */
    @GetMapping("/heapSort")
    public RespData heapSort(String nums) {
        Integer[] numArr = getNumArrs(nums);
        SortUtil.heapSort(numArr);
        return RespData.getSuccess(CollUtil.reverse(Arrays.asList(numArr)));
    }
}
